package com.teamtwoseven.todo;

import java.util.regex.Pattern;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * This screen allows the user to create a new account.
 * 
 * @author teamtwoseven
 */
public class NewAccount extends Activity implements OnClickListener {

	private Button bJoin, bCancel;
	private EditText etFullName, etUserID, etPassword, etEmail;
	private Storage storage;

	/**
	 * Called when the activity is first created.
	 * 
	 * @param savedInstanceState
	 * 			allows instance data to be stored
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_account);

		bJoin = (Button) findViewById(R.id.newAccount_bJoin);
		bCancel = (Button) findViewById(R.id.newAccount_bCancel);
		etFullName = (EditText) findViewById(R.id.newAccount_etFullName);
		etUserID = (EditText) findViewById(R.id.newAccount_etUserID);
		etPassword = (EditText) findViewById(R.id.newAccount_etPassword);
		etEmail = (EditText) findViewById(R.id.newAccount_etEmail);

		bJoin.setOnClickListener(this);
		bCancel.setOnClickListener(this);

		storage = new Storage(NewAccount.this);
	}

	/**
	 * Handles button clicks
	 * 
	 * @param arg0
	 * 			the element that was clicked
	 */
	public void onClick(View arg0) {
		if (arg0 == bJoin) {
			String fullName = etFullName.getText().toString();
			String userID = etUserID.getText().toString();
			String password = etPassword.getText().toString();
			String email = etEmail.getText().toString();

			if (verify(fullName, userID, password, email)) {
				storage.createUser(userID, password, fullName, email);

				startActivity(new Intent("com.teamtwoseven.todo.SUCCESSFUL"));
				finish();
			}
		}
		else if (arg0 == bCancel) {
			// End this Activity and Return to Login Screen
			finish();
		}
	}

	/**
	 * Verifies the given parameters are valid and the userID is unique. If
	 * not, displays why.
	 * 
	 * @param fullName
	 * 			the user's full name
	 * @param userID
	 * 			the user's chosen userID
	 * @param password
	 * 			the user's chosen password
	 * @param email
	 * 			the user's email address
	 * @return true if all parameters are valid, false otherwise
	 */
	private boolean verify(String fullName, String userID, String password,
		String email) {
		
		boolean success = true;
		String feedback = "";

		if (!validText(fullName)) {
			feedback += "Enter your name\n";
			success = false;
		}
		if (!validText(userID)) {
			feedback += "Enter a user ID\n";
			success = false;
		}
		else if (!uniqueID(userID)) {
			feedback += "User ID already in use\n";
			success = false;
		}
		if (!validText(password) || password.length() < 6) {
			feedback += "Password must be at least 6 characters\n";
			success = false;
		}
		if (!validEmail(email)) {
			feedback += "Invalid email address";
			success = false;
		}
		if (!success) {
			// Display which parameters are invalid
			popupDialog(feedback);
		}
		return success;
	}

	/**
	 * Verifies the given text is not empty or all whitespace.
	 * 
	 * @param text
	 * 			a String
	 * @return true if the text is not empty or whitespace, false otherwise
	 */
	private boolean validText(String text) {
		return !text.replaceAll("\\s+", "").equals("");
	}

	/**
	 * Checks if the given userID is already in use.
	 * 
	 * @param userID
	 * 			the userID in question
	 * @return true if the userID does not exist, false if it does
	 */
	private boolean uniqueID(String userID) {
		long rowID = storage.getRowID(userID);

		return (rowID == -1);
	}
	
	/**
	 * Validates the given email address.
	 * 
	 * @param email
	 * 			the email address in question
	 * @return true if the email address is valid, false if not
	 */
	public boolean validEmail(String email) {		
		return Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@" +
			"[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$").matcher(
			email).matches();
	}

	/**
	 * Displays the given message and an OK button in a dialog.
	 * 
	 * @param message
	 * 			text to display to the user
	 */
	private void popupDialog(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setMessage(message);
		builder.setCancelable(false);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.cancel();
			}
		});
		builder.show();
	}
}